Shader "Unlit/BumpShder漫反射+高光+纹理+法线纹理"
{
    Properties
	{
		// 主纹理
		_MainTex ("主纹理", 2D) = "white"{}
		// 法线纹理
		_BumpTex ("法线纹理", 2D) = "white"{}
		// 漫反射颜色/强度
		_Diffuse ("漫反射颜色/强度", COLOR) = (1,1,1,1)
		// 高光颜色/强度
		_Specular ("高光颜色/强度", COLOR) = (1,1,1,1)
		// 高光区域大小
		_Gloss ("高光区域大小", Range(3, 256)) = 20
		// 法线缩放
		_BumpScale ("法线缩放", Range(0,10)) = 1
	}

	SubShader
	{
		Pass
		{
			Tags{"LightMode"="ForwardBase"}

			CGPROGRAM

			#pragma vertex vert
			#pragma fragment frag

			#include "UnityCG.cginc"
			#include "Lighting.cginc"

			// 获取材质变量信息
			sampler2D _MainTex;
			sampler2D _BumpTex;
			float4 _MainTex_ST;
			float4 _BumpTex_ST;
			float4 _Diffuse;
			float4 _Specular;
			float _Gloss;
			float _BumpScale;

			// 输入
			struct a2v
			{
				float4 vertex : POSITION;
				float4 normal : NORMAL0;
				float4 tangent : TANGENT;
				float2 uv : TEXCOORD0;
			};

			// 输出
			struct v2f
			{
				fixed4 vertex : SV_POSITION;
				fixed4 uv : TEXCOORD1;
				float4 TtoW0 : TEXCOORD2;
				float4 TtoW1 : TEXCOORD3;
				float4 TtoW2 : TEXCOORD4;
			};

			// 顶点着色
			v2f vert(a2v a)
			{
				v2f f;
				f.vertex = UnityObjectToClipPos(a.vertex);
				f.uv.xy = TRANSFORM_TEX(a.uv, _MainTex);
				f.uv.zw = TRANSFORM_TEX(a.uv, _BumpTex);

				// 世界空间下的顶点/法线/切线/副切线
				fixed3 worldNormal = UnityObjectToWorldNormal(a.normal);
				fixed3 worldVertex = mul(unity_ObjectToWorld, a.vertex).xyz;
				fixed3 worldTragen = UnityObjectToWorldDir(a.tangent.xyz);
				fixed3 worldBinormal = cross(worldNormal, worldTragen) * a.tangent.w;

				// 创建一个转换矩阵 按列排序 (世界)切线、(世界)副切线、(世界)法线、(世界)位置
				f.TtoW0 = float4(worldTragen.x, worldBinormal.x, worldNormal.x, worldVertex.x);
				f.TtoW1 = float4(worldTragen.y, worldBinormal.y, worldNormal.y, worldVertex.y);
				f.TtoW2 = float4(worldTragen.z, worldBinormal.z, worldNormal.z, worldVertex.z);

				return f;
			}

			// 片元着色
			fixed4 frag(v2f v) : SV_Target
			{
				// 得到插值过后的世界空间中的顶点位置（位置在矩阵中的最后一列）
				fixed3 worldPos = fixed3(v.TtoW0.w, v.TtoW1.w, v.TtoW2.w);


				float3 worldLightDir = normalize(UnityWorldSpaceLightDir(worldPos));
				float3 worldViewDir = normalize(UnityWorldSpaceViewDir(worldPos));

				fixed3 normalBump = UnpackNormal(tex2D(_BumpTex, v.uv.zw));
				normalBump.xy *= _BumpScale;
				normalBump.z = sqrt(1.0 - saturate(dot(normalBump.xy, normalBump.xy)));
				// 用矩阵对法线贴图进行转换
				normalBump = normalize(half3(dot(v.TtoW0.xyz, normalBump), dot(v.TtoW1.xyz, normalBump), dot(v.TtoW2.xyz, normalBump)));

				// 环境光
				float3 ambients = UNITY_LIGHTMODEL_AMBIENT;

				// 漫反射
				// 半兰伯特
				float3 halfLambert = dot(normalBump, worldLightDir) * 0.5 + 0.5;
				float3 diffuse = _LightColor0.rgb * _Diffuse.rgb * halfLambert;

				// 高光
				float3 halfDir = normalize(worldLightDir + worldViewDir);
				float3 blinn = pow(saturate(dot(normalBump, halfDir)), _Gloss);
				float3 specular = _LightColor0.rgb * _Specular.rgb * blinn;

				// 光颜色
				float3 color = ambients + diffuse + specular;
				// 主纹理采样
				float3 texColor = tex2D(_MainTex, v.uv).rgb;

				return fixed4(color * texColor, 1);
			}

			ENDCG
		}
	}
}
